//
//  CollectionViewCell.m
//  ArandaTest
//
//  Created by Camilo Cadena on 2/18/15.
//  Copyright (c) 2015 Camilo Cadena. All rights reserved.
//

#import "CollectionViewCell.h"
#import "UIImageView+AFNetworking.h"
#import "ServicesStrings.h"
#import "test1HeaderConstants.h"

@implementation CollectionViewCell

// Updates cell content information and image
-(void)updateCell:(SeriesObject *)item{
    [self.cellTitle setText:[item.name uppercaseString]];
    
    if([item.poster_path isKindOfClass:[NSNull class]] || item.poster_path.length<=0 || item.poster_path == nil){
        //no image to load, put placeholder instead
        UIImage *placeholderImage = [UIImage imageNamed:@"placeholder"];
        self.image.image = placeholderImage;
        self.image.contentMode = UIViewContentModeCenter;
    }else{
        [self loadImage:[NSString stringWithFormat:KIMAGE_URL,item.poster_path]];
    }
    
}

//Rquest the image
-(void)loadImage:(NSString *)urlImage{
    
    NSURL *url = [NSURL URLWithString:urlImage];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIImage *placeholderImage = [UIImage imageNamed:@"placeholder"];
    
    [self setNeedsDisplay];
    self.image.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.image setClipsToBounds:YES];
    [self clipsToBounds];
    
    
    [self.image setImageWithURLRequest:request placeholderImage:placeholderImage
                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                            
                                   self.image.contentMode = UIViewContentModeTop;
                                   self.image.image = image;
                               } failure:nil];
    
}

@end